package cn.zyx.pet.controller;

import cn.zyx.basic.util.AjaxResult;
import cn.zyx.basic.util.LoginContext;
import cn.zyx.basic.util.PageList;
import cn.zyx.pet.domain.Pet;
import cn.zyx.pet.domain.SearchMasterMsg;
import cn.zyx.pet.query.SearchMasterMsgQuery;
import cn.zyx.pet.service.ISearchMasterMsgService;
import cn.zyx.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.getAll();
    }

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            // 使用工具类 - 获取到当前登录人的用户信息
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            // 把当前发布的信息和用户信息的id传到service
            searchMasterMsgService.publish(searchMasterMsg,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发布失败!");
        }
    }

    /**
     * 待处理消息查询接口
     *      平台管理
     *      店铺员工（店铺管理员 + 员工）
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            // 使用工具类 - 获取到当前登录人的用户信息
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            // 把当前发布的信息和用户信息的id传到service
            return searchMasterMsgService.pending(searchMasterMsgQuery,loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 已处理消息接口
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            // 使用工具类 - 获取到当前登录人的用户信息
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            // 把当前发布的信息和用户信息的id传到service
            return searchMasterMsgService.processed(searchMasterMsgQuery,loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 前端用户查询自己消息的接口
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUser(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        try {
            // 使用工具类 - 获取到当前登录人的用户信息
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            // 把当前发布的信息和用户信息的id传到service
            return searchMasterMsgService.queryUser(searchMasterMsgQuery,loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *  处理消息
     * @param pet
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.handle(pet,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发布失败!");
        }
    }
}
